package cn.jit.controller;

import java.util.ArrayList;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import cn.jit.po.Question;
import cn.jit.service.QuestionService;
import cn.jit.util.ResponseObject;
import cn.jit.util.ResponseUtil;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/question")
public class QuestionController {
	
	@Resource
    private QuestionService questionService;
	
	@RequestMapping("/back_all")
	public void getQuestions(Question question,HttpServletResponse response) {
		
		ArrayList<Question> questionList = (ArrayList<Question>) questionService.getAllQuestions(question);
		
		Gson gson = new GsonBuilder().create();
        
		
        String resp = gson.toJson(new ResponseObject(200,true,"success",questionList));
		
        System.out.println(resp);
        
		JSONObject jsonObject = JSONObject.fromObject(new ResponseObject(200,true,"success",questionList));
		ResponseUtil.writeJson(response, jsonObject);
		
		return ;
	}
	
	@RequestMapping(value="/back_addOne",method=RequestMethod.POST)
	public void addOneQuestion(Question question,HttpServletResponse response) {
		
		Boolean result = questionService.addOneQuestion(question);
		
		if (!result) {
			JSONObject jsonObject = JSONObject.fromObject(new ResponseObject(200,false,"新增失败！",null));
			ResponseUtil.writeJson(response, jsonObject);
			return;
		}

		JSONObject jsonObject = JSONObject.fromObject(new ResponseObject(200,true,"新增成功！",null));
		ResponseUtil.writeJson(response, jsonObject);
		return;
	}
	
}
